package bdz.common.display
{
	import bdz.common.events.GenericEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class BrushCanvas extends MovieClip {
		private var _renderer:Object;
		private var _duplicate:Boolean = false;
		private var _activated:Boolean = false;
		private var _oldPos:Point = new Point(0, 0);
		private var _activeClip:MovieClip;
		public function BrushCanvas() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			
		}
		private function initializeComponent(evt:Event):void{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onBrushMove);
			if(!_duplicate){
				_activeClip = new _renderer() as MovieClip;
				_activeClip.visible = false;
				addChild(_activeClip);
			}
			
		}
		private function onBrushMove(evt:MouseEvent):void{
			if(!_activated) return;
			
			var dx:Number = stage.mouseX-_oldPos.x;
			var dy:Number = stage.mouseY-_oldPos.y;
			
			
			_activeClip.rotation = (Math.atan2(dy, dx))*180/Math.PI;
			_activeClip.x = stage.mouseX;
			_activeClip.y = stage.mouseY;
			
			dispatchEvent(new GenericEvent(GenericEvent.CHANGE, {x:dx, y:dy}));
			
			
			
		}
		public function activate(b:Boolean):void{
			_oldPos = new Point(stage.mouseX, stage.mouseY);
			_activated = b;
			_activeClip.alpha = 0;
			_activeClip.visible = _activated;
		}
		public function set renderer(r:Object):void{
			_renderer = r;
		}
		public function get renderer():Object{
			return _renderer;
		}
		public function set duplicate(b:Boolean):void{
			_duplicate = b;
		}
		public function get duplicate():Boolean{
			return _duplicate;
		}
		public function get currentBrush():MovieClip{
			return _activeClip;
		}

	}
}